<?php
class Manager_NewsSolutionController extends Zendvn_Controller_Action{
	private $_arrParam;
	
	private $_mainAction;
	
	protected $_currentController;
	
	protected $_paginator = array(
									'itemCountPerPage' => 5,
									'pageRange' => 3,
								  ); 
	
	public  function init()
	{
	    $this->_arrParam = $this->_request->getParams();	
        $this->loadTemplate(TEMPLATE_PATH . '/admin/system');

        
        $this->_mainAction = $this->_arrParam['module'] . '/' . $this->_arrParam['controller'] . '/index';	
	    $this->view->arrParam = $this->_arrParam; 
        
        $ns = new Zend_Session_Namespace('login'); 
	    if(($ns->id)<1) 
	      {
	           $this->_redirect('manager/');
	      }      
	     $tbl_new                   = new Manager_Model_newssolution();
	 	 $this->view->seclist       = $tbl_new->listsec();
		 $this->view->catlist       = $tbl_new->listcat();
		 $this->view->list_product  = $tbl_new->listproduct();
		
		 $tbl_menu                  = new Manager_Model_menu();
	     $this->view->language      = $tbl_menu->get_language();
	  
	}
	
	public function indexAction()
	{
	 
	   $this->view->Title = "Article Manager ";
		$this->view->headTitle($this->view->Title,true);

        $this->view->headScript()->appendFile($this->view->baseUrl().'/js/balloontip.js');
	    $this->view->headLink()->appendStylesheet($this->view->baseUrl().'/css/balloontip.css');
		
		$tbl_new=new Manager_Model_newssolution();
		
	    if($this->_request->getCookie('catnews')==NULL){ $catnews=-1; } 
			else { $catnews=$this->_request->getCookie('catnews');}	 
			
		if($this->_request->getCookie('secnews')==NULL){ $secnews=-1; } 
			else { $secnews=$this->_request->getCookie('secnews');}	 
			
	    $data_cat             = $tbl_new->cat_id_cat($secnews);
		$this->view->data_cat = $data_cat;
		
		if($this->_request->getPost('keyword')!=NULL){
			 $keyword=$this->_request->getParam('keyword');
			 $this->view->keyword=$keyword;
			
			 if($this->_request->getCookie('status')==NULL){ $status=-1; } 
			 else { $status=$this->_request->getCookie('status');}

     	 	 $listArticles = $tbl_new->searchkq($keyword,$status,$catnews,$secnews);
          	 $currentPage = 1;
         	 $i = $this->_getParam('page',1);
         	 if(!empty($i))
         	 {
				 $currentPage = $i;
          	 }
         	 $paginator = Zend_Paginator::factory($listArticles);
         	 $paginator->setItemCountPerPage(40)
                    ->setPageRange(5)
                    ->setCurrentPageNumber($currentPage);
         	 $this->view->paginator = $paginator;
		 } 
    	else 
         {
			if($this->_request->getCookie('status')==NULL) {$status=-1;} 
			else {$status=$this->_request->getCookie('status');}
			 		
		     	 	$listArticles = $tbl_new->listItemabout($status,$catnews,$secnews);
		     	
		     	 	
		     	 	$currentPage = 1;
		         	 $i = $this->_getParam('page',1);
		         	 if(!empty($i))
		         	 {
		          	    $currentPage = $i;
		          	 }
         	 $paginator = Zend_Paginator::factory($listArticles);
         	 $paginator->setItemCountPerPage(45)
                    ->setPageRange(10)
                    ->setCurrentPageNumber($currentPage);
         	 $this->view->paginator = $paginator;
	     
          }
		
}
		
  	public function statusAction(){
		
   $db = Zend_Registry::get('connectDb');
   $arrParam=$this->_arrParam;
   if($this->_arrParam['type'] == 'multi'){
		    
			if(count($arrParam['cid'])>0){
				$ids = implode(',', $arrParam['cid']);
				$status = ($arrParam['s'] == 0 )? 1:0;  
				$where = 'id IN (' . $ids . ')'; 
			    $db->update('article_solution',array('published'=>$status),$where);
			}
			 }
	else 
	     {
			$status = ($arrParam['s'] == 0 )? 1:0;            
			$where ="id=$arrParam[id]";  
			$db->update('article_solution',array('published'=>$status),$where);			
		   	 
	        }
	header("location:".$_SERVER['HTTP_REFERER']);
	 $this->_helper->viewRenderer->setNoRender();
	  }
	  
	public function sortAction(){ 
		$db = Zend_Registry::get('connectDb');
		if($this->_request->isPost()){
		  
		foreach ($this->_arrParam['cid'] as $val){ 
		$where = 'id = ' . $val;
		$db->update('article_solution',array('order'=>$this->_arrParam['order'][$val]),$where);
		
		}
		     header("location:".$_SERVER['HTTP_REFERER']);
		     $this->_helper->viewRenderer->setNoRender();
		} 
}
	
    public function addAction()
	{
	  $this->view->headScript()->appendFile($this->view->baseUrl().'/ckeditor/ckeditor.js');
	  $this->view->headScript()->appendFile($this->view->baseUrl().'/ckfinder/ckfinder.js');
	  
	  $this->view->Title = "Article Manager [New]";
	  $this->view->headTitle($this->view->Title,true);
	  $tblCountry = new Manager_Model_newssolution();
	  
	 if($this->_request->isPost()==true)
		{	
		   $data      =$this->_arrParam;
		   if ($data['title']=='')
		   {
		   	  $error=1;
		      $this->view->error  ='Enter title article';
		   }
		   elseif ($data['section_id']=='0')
		     {
		      $error=1;
		      $this->view->error  ='Enter title section';
		     }
		   elseif ($data['cat_id']=='0')
		     {
		      $error=1;
		      $this->view->error  ='Enter title category';
		     }
		 elseif ($data['language_id']=='0')
		     {
		      $error=1;
		      $this->view->error  ='Select language';
		     }
		 if (empty($error)==true)
		    {
		     $tblCountry->save($data);	
		     if($this->_request->getParam('id')>0)
		        {
		            $id=$this->_request->getParam('id');
		        }
		      else 
		        {
		          $id = $tblCountry->layid();		   
		        }
		        $tblCountry->delete_item($id);
		        $tblCountry->save_itemt($data,$id);
	            $this->_redirect('manager/news-solution');	
		    }
		}
	
	}
		
    public function editAction()
	{
	  $this->view->headScript()->appendFile($this->view->baseUrl().'/ckeditor/ckeditor.js');
	  $this->view->headScript()->appendFile($this->view->baseUrl().'/ckfinder/ckfinder.js');
	  
	  $this->view->Title = "Article Manager [Edit]";
	  $this->view->headTitle($this->view->Title,true);
	  
	  $tbl_new  = new Manager_Model_newssolution();
	  $id=$this->_request->getParam('id');
	  $this->view->result = $tbl_new->getrow($id);
	  $this->view->activiti      = $tbl_new->listproduct_activiti($id);
	  
	  if ($this->_request->isPost())  
		  {
		   $data      =$this->_arrParam;
		   if ($data['title']=='')
		   {
		   	  $error=1;
		      $this->view->error  ='Enter you title';
		   }
		   elseif ($data['section_id']=='0')
		     {
		      $error=1;
		      $this->view->error  ='Select section';
		     }
		   elseif ($data['cat_id']=='0')
		     {
		      $error=1;
		      $this->view->error  ='Select you category';
		     }
		   elseif ($data['language_id']=='0')
		     {
		      $error=1;
		      $this->view->error  ='Select language';
		     }
		 if (empty($error)==true)
		    {
		     $tbl_new->save($data);	
	         $this->_redirect('manager/news-solution');	
		    }
		  }
	
	}

    public  function deleteAction()
	{
		$res = new Manager_Model_newssolution();
		$id=$this->_request->getParam('id');
		$res->delete_data($id);
		$this->_redirect('manager/news-solution');	
		$this->_helper->viewRenderer->setNoRender();
	}
    public function mutiAction()
	{
	    $tblsection=new Manager_Model_newssolution();
		$tblsection->delete_muti($this->_arrParam);
		$this->_redirect('manager/news-solution');	
		$this->_helper->viewRenderer->setNoRender();
	}

    public function specialAction(){
		
   $db = Zend_Registry::get('connectDb');
   $arrParam=$this->_arrParam;
   if($this->_arrParam['type'] == 'multi'){
		    
			if(count($arrParam['cid'])>0){
				$ids = implode(',', $arrParam['cid']);
				$status = ($arrParam['s'] == 0 )? 1:0;  
				$where = 'id IN (' . $ids . ')'; 
			    $db->update('solution',array('special'=>$status),$where);
			}
			 }
	else 
	     {
			$status = ($arrParam['s'] == 0 )? 1:0;            
			$where ="id=$arrParam[id]";  
			$db->update('solution',array('special'=>$status),$where);			
		   	 
	        }
	
	 header("location:".$_SERVER['HTTP_REFERER']);
	 $this->_helper->viewRenderer->setNoRender();
	  }	  
    
    public function featuredAction(){
		
   $db = Zend_Registry::get('connectDb');
   $arrParam=$this->_arrParam;
   if($this->_arrParam['type'] == 'multi'){
		    
			if(count($arrParam['cid'])>0){
				$ids = implode(',', $arrParam['cid']);
				$status = ($arrParam['s'] == 0 )? 1:0;  
				$where = 'id IN (' . $ids . ')'; 
			    $db->update('article_solution',array('featured'=>$status),$where);
			}
			 }
	else 
	     {
			$status = ($arrParam['s'] == 0 )? 1:0;            
			$where ="id=$arrParam[id]";  
			$db->update('article_solution',array('featured'=>$status),$where);			
		   	 
	        }
	 header("location:".$_SERVER['HTTP_REFERER']);
	 $this->_helper->viewRenderer->setNoRender();
	  }
	  
	  
}